package value

import (
	"context"
	"fmt"
	"github.com/samber/lo"
	"strings"
	"tongquetai_server/activity_vk/infrastructure/utils"
)

// @META.table:json
/*
[
    ["活动商店配置"],
    ["货币种类", "货币数量", "限购数量", "是否每日重置", "道具内容"],
    ["金币", 100, 100, "是", "鼓舞旗子1级礼包*1"],
    ["食物", 100, 100, "否", "鼓舞旗子1级礼包*1"]
]
*/
// @META.struct:tqt
type TqtActivityStore struct {
	Currency      TqtItem  `json:"currency" header:"货币种类" validate:"required"`
	CurrencyCount int      `json:"currencyCount" header:"货币数量" validate:"gt=0"`
	BuyCountLimit int      `json:"buyCountLimit" header:"限购数量" validate:"gt=0"`
	IsDailyReset  bool     `json:"isDailyReset" header:"是否每日重置" true:"是" false:"否"`
	Award         TqtAward `json:"award" header:"道具内容" validate:"required"`
}

func (t *TqtActivityStore) Call(ctx context.Context) (err error) {
	// 金币、食物、碎金
	currencyTypeList := []string{CurrencyItemId, FoodItemId, ScrapGoldId}

	// 获取相应的道具名称
	var itemName string
	currencyTypeNameList := make([]string, 0, len(currencyTypeList))
	for _, itemId := range currencyTypeList {
		itemName = utils.GetItemNameByItemId(ctx, itemId)
		currencyTypeNameList = append(currencyTypeNameList, itemName)
	}
	currencyTypeNameStr := strings.Join(currencyTypeNameList, "、")

	// 校验
	if !lo.Contains(currencyTypeList, t.Currency.ItemId) {
		return fmt.Errorf("货币类型只能填写%s", currencyTypeNameStr)
	}

	return
}
// @META.struct:svr
type SvrActivityStore struct {
	No            int      `json:"no"`
	Price         SvrAward `json:"price"`
	BuyCountLimit int      `json:"buy_count_limit"`
	IsDailyReset  bool     `json:"is_daily_reset"`
	Award         SvrAward `json:"award"`
}

/*
铜雀台 转 服务端
*/

func TqtToSvrActivityStore(activityStore TqtActivityStore, no int) SvrActivityStore {
	return SvrActivityStore{
		No: no,
		Price: TqtToSvrAward(TqtAward{
			TqtItem:   activityStore.Currency,
			ItemCount: activityStore.CurrencyCount,
		}),
		BuyCountLimit: activityStore.BuyCountLimit,
		IsDailyReset:  activityStore.IsDailyReset,
		Award:         TqtToSvrAward(activityStore.Award),
	}
}

func TqtToSvrActivityStoreList(activityStores []TqtActivityStore, no int) []SvrActivityStore {
	res := make([]SvrActivityStore, len(activityStores))
	for i, v := range activityStores {
		res[i] = TqtToSvrActivityStore(v, no)
		no += 1
	}

	return res
}

/*
服务端 转 铜雀台
*/

func SvrToTqtActivityStore(ctx context.Context, activityStore SvrActivityStore) TqtActivityStore {
	return TqtActivityStore{
		Currency: SvrToTqtItem(ctx, SvrItem{
			GameObjectId: activityStore.Price.GameObjectId,
		}),
		CurrencyCount: activityStore.Price.Count,
		BuyCountLimit: activityStore.BuyCountLimit,
		IsDailyReset:  activityStore.IsDailyReset,
		Award:         SvrToTqtAward(ctx, activityStore.Award),
	}
}

func SvrToTqtActivityStoreList(ctx context.Context, activityStores []SvrActivityStore) []TqtActivityStore {
	res := make([]TqtActivityStore, len(activityStores))
	for i, v := range activityStores {
		res[i] = SvrToTqtActivityStore(ctx, v)
	}

	return res
}
